package io.wmy.supex.framework.support.spring.view;

import org.apache.poi.ss.usermodel.*;
import org.springframework.web.servlet.view.document.AbstractXlsView;
import io.wmy.supex.common.bean.MyContent;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * Created by qiaoming on 2016/8/5.
 */

/**
 * AbstractExcelView已废弃
 */
public class XlsView extends AbstractXlsView {

    @Override
    protected void buildExcelDocument(Map<String, Object> map, Workbook workbook, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {

        Sheet sheet = workbook.createSheet("My Content");
        Row row = sheet.createRow(0);
        CellStyle style = workbook.createCellStyle();
        style.setFillBackgroundColor(IndexedColors.YELLOW.getIndex());
        style.setFillPattern(CellStyle.ALIGN_FILL);
        Font font = workbook.createFont();
        font.setColor(IndexedColors.RED.getIndex());
        style.setFont(font);
        Cell cell1 = row.createCell(0);
        cell1.setCellValue("ID");
        cell1.setCellStyle(style);
        Cell cell2 = row.createCell(1);
        cell2.setCellValue("TITLE");
        cell2.setCellStyle(style);
        Cell cell3 = row.createCell(2);
        cell3.setCellValue("DESCRIPTION");
        cell3.setCellStyle(style);
        Cell cell4 = row.createCell(3);
        cell4.setCellValue("PUBDATE");
        cell4.setCellStyle(style);
        Cell cell5 = row.createCell(4);
        cell5.setCellValue("LINK");
        cell5.setCellStyle(style);
        Object ob = map.get("contents");
        if (ob instanceof List) {
            for (int i = 0; i < ((List<?>) ob).size(); i++) {
                Object feedObj = ((List<?>) ob).get(i);
                MyContent myContent = (MyContent) feedObj;
                row = sheet.createRow(i + 1);
                cell1 = row.createCell(0);
                cell1.setCellValue(myContent.getId());
                cell2 = row.createCell(1);
                cell2.setCellValue(myContent.getTitle());
                cell3 = row.createCell(2);
                cell3.setCellValue(myContent.getDescription());
                cell4 = row.createCell(3);
                cell4.setCellValue(myContent.getPubDate());
                cell5 = row.createCell(4);
                cell5.setCellValue(myContent.getLink());
            }
        }
    }

}